package vn.ugame.model.bl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import vn.ugame.exception.DataConnectionFailed;
import vn.ugame.exception.InvalidEntityClass;
import vn.ugame.model.dal.ModuleDA;
import vn.ugame.model.entity.Module;

public class ModuleManager {

    public ModuleManager() {
    }

    public Module findModuleByName(String name)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        ModuleDA moduleDA = new ModuleDA();
        return moduleDA.findModuleByName(name);
    }

    public Module findLocalModule(String name)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        ModuleDA moduleDA = new ModuleDA();
        return moduleDA.findLocalModuleByName(name);
    }

    public List<Module> findRootModules(Locale locale)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        ModuleDA moduleDA = new ModuleDA();
        return moduleDA.findLocalModules(null, null, null, null, Boolean.TRUE, locale);
    }

    public List<Module> findAllModules()
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        ModuleDA moduleDA = new ModuleDA();

        List<Module> modules = new ArrayList<Module>();

        List<Module> temp = moduleDA.findLocalModules(null, null, null, null, null, null);
        for (Module module : temp) {
            if (!modules.contains(module)) {
                modules.add(module);
            }
        }

        temp = moduleDA.findModules(null, null, null, null, null);
        for (Module module : temp) {
            if (!modules.contains(module)) {
                modules.add(module);
            }
        }

        return modules;
    }

    public List<Module> findAllLocalModules()
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        ModuleDA moduleDA = new ModuleDA();
        return moduleDA.findLocalModules(null, null, null, null, null, null);
    }

    public List<Module> findLocalModules(int type, Locale locale)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        ModuleDA moduleDA = new ModuleDA();
        return moduleDA.findLocalModules(null, null, null, type, null, locale);
    }

    public List<Module> findLocalModules(String id)
            throws DataConnectionFailed, SQLException, InvalidEntityClass {
        ModuleDA moduleDA = new ModuleDA();
        return moduleDA.findLocalModules(id, null, null, null, null, null);
    }
}
